{% extends 'base.html' %}

{% block title %}使用记录 - 维修管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>使用记录</h2>
    <a href="{% url 'spare_parts:usage_add' %}" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> 添加使用记录
    </a>
</div>

<!-- 搜索过滤 -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-6">
                <label class="form-label">搜索</label>
                <div class="input-group">
                    <input type="text" name="q" class="form-control" placeholder="搜索备件、使用人或工单..." value="{{ search_query }}">
                    <button type="submit" class="btn btn-outline-secondary">
                        <i class="bi bi-search"></i>
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 使用记录列表 -->
<div class="card">
    <div class="card-body">
        {% if usage_records %}
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>备件名称</th>
                        <th>型号</th>
                        <th>数量</th>
                        <th>使用人</th>
                        <th>关联工单</th>
                        <th>用途说明</th>
                        <th>使用时间</th>
                    </tr>
                </thead>
                <tbody>
                    {% for usage in usage_records %}
                    <tr>
                        <td>{{ usage.spare_part.name }}</td>
                        <td>{{ usage.spare_part.model }}</td>
                        <td>{{ usage.quantity }} {{ usage.spare_part.unit }}</td>
                        <td>{{ usage.used_by.username }}</td>
                        <td>
                            {% if usage.ticket %}
                            <a href="{% url 'tickets:ticket_detail' usage.ticket.pk %}">
                                {{ usage.ticket.title }}
                            </a>
                            {% else %}
                            -
                            {% endif %}
                        </td>
                        <td>{{ usage.purpose|truncatewords:5 }}</td>
                        <td>{{ usage.used_at|date:"Y-m-d H:i" }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="text-center py-5">
            <i class="bi bi-clipboard-check display-1 text-muted"></i>
            <h5 class="mt-3">暂无使用记录</h5>
            <p class="text-muted">点击"添加使用记录"开始记录第一个备件使用</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
